Pandas时间序列:时期(period)及其算术运算

您所在的位置:网站首页 什么是grace period Pandas时间序列:时期(period)及其算术运算

Pandas时间序列:时期(period)及其算术运算

2023-11-25 23:26| 来源: 网络整理| 查看: 265

import pandas as pd import numpy as np 一、时间类型及其在python中对应的类型 时间戳–timestamp时间间隔–timedelta时期–period 二、时期

时期表示的是时间区间,比如数日、数月、数季、数年等

1.定义一个Period p = pd.Period(2007,freq='A-DEC') #表示以12月作为结束的一整年,这里表示从2007-01-01到2017-12-31的全年 p Period('2007', 'A-DEC') 2.通过加减整数可以实现对Period的移动 p+5 Period('2012', 'A-DEC') p-2 Period('2005', 'A-DEC') 3.如果两个Period对象拥有相同频率,则它们的差就是它们之间的单位数量 pd.Period('2014',freq='A-DEC') - p 7 4.period_range函数可用于创建规则的时期范围 rng = pd.period_range('1/1/2000','6/30/2000',freq='M') #创建从2001-01-01到2000-06-30所有月份的Period pd.Series(np.random.randn(6),index=rng) 2000-01 -1.125053 2000-02 1.035250 2000-03 -0.796830 2000-04 0.381285 2000-05 0.533522 2000-06 -2.733462 Freq: M, dtype: float64 5.PeriodIndex类的构造函数允许直接使用一组字符串表示一段时期 values = ['2001Q3','2002Q2','2003Q1'] index = pd.PeriodIndex(values,freq='Q-DEC') index PeriodIndex(['2001Q3', '2002Q2', '2003Q1'], dtype='period[Q-DEC]', freq='Q-DEC') 三、时期的频率转换-asfreq 1.通过asfreq可以将频率转换 p = pd.Period('2007',freq='A-DEC') # 2007年1月1日到2007年12月31日 p.asfreq('M',how='start') # 将评率为年(20070101-20071231)转换频率为月201701 Period('2007-01', 'M') p.asfreq('M',how='end') # 将评率为年(20070101-20071231)转换频率为月201712 Period('2007-12', 'M') 2.不同频率经过asfreq转换后的结果不同 p = pd.Period('2007',freq='A-JUN') # 2006年7月1日到2007年6月30日 p.asfreq('D','start') Period('2006-07-01', 'D') p.asfreq('D','end') Period('2007-06-30', 'D') 3.从高频率转换为低频率时,超时期(较大的时期)是由子时期(较小的时期)的位置绝对的 p = pd.Period('2007-08','M') p.asfreq('A-JUN') # 200708对于频率A-JUN是属于2008年度的 Period('2008', 'A-JUN') 4.对于PeriodIndex或TimeSeries的频率转换方式相同 rng = pd.period_range('2006','2009',freq='A-DEC') ts = pd.Series(np.random.randn(len(rng)),index=rng) ts 2006 -1.202858 2007 -1.132553 2008 0.902564 2009 0.800859 Freq: A-DEC, dtype: float64 ts.asfreq('M',how='start') 2006-01 -1.202858 2007-01 -1.132553 2008-01 0.902564 2009-01 0.800859 Freq: M, dtype: float64 ts.asfreq('B',how='end') 2006-12-29 -1.202858 2007-12-31 -1.132553 2008-12-31 0.902564 2009-12-31 0.800859 Freq: B, dtype: float64 四、按季度计算的时期频率

许多季度型数据会涉及“财年末”的概念,通常是一年12个月中某月的最后一个工作日或日历日。因此,时间“2012Q4”根据财年末的不同会有不同的含义。pandas支持12种可能的季度型频率,即Q-JAN到Q-DEC。

1.财政年度和季度 p = pd.Period('2012Q4',freq='Q-JAN') # Q-JAN是指1月末的工作日是财政年末 p Period('2012Q4', 'Q-JAN') p.asfreq('D','start') Period('2011-11-01', 'D') p.asfreq('D','end') Period('2012-01-31', 'D') 2.该季度倒数第二个工作日的下午4点 p4pm = (p.asfreq('B','e')-1).asfreq('T','s')+16*60 p4pm.to_timestamp() Timestamp('2012-01-30 16:00:00') 3.相同的运算可以应用到TimeSeries rng = pd.period_range('2011Q3','2012Q4',freq='Q-JAN') ts = pd.Series(np.arange(len(rng)),index=rng) ts 2011Q3 0 2011Q4 1 2012Q1 2 2012Q2 3 2012Q3 4 2012Q4 5 Freq: Q-JAN, dtype: int32 new_rng = (rng.asfreq('B','e')-1).asfreq('T','s')+16*60 ts.index = new_rng.to_timestamp() ts 2010-10-28 16:00:00 0 2011-01-28 16:00:00 1 2011-04-28 16:00:00 2 2011-07-28 16:00:00 3 2011-10-28 16:00:00 4 2012-01-30 16:00:00 5 dtype: int32 五、Timestamp与Period互相转换 1.通过to_period方法,可以将时间戳(timestamp)索引的Series和DataFrame对象转换为以时期(period)索引 rng = pd.date_range('1/1/2000',periods=3,freq='M') ts = pd.Series(np.random.randn(3),index=rng) ts 2000-01-31 -0.501502 2000-02-29 -1.299610 2000-03-31 -0.705091 Freq: M, dtype: float64 pts = ts.to_period() pts 2000-01 -0.501502 2000-02 -1.299610 2000-03 -0.705091 Freq: M, dtype: float64 2.将timestamp转换为period是运行重复的 rng = pd.date_range('1/29/2000',periods=6,freq='D') ts2 = pd.Series(np.random.randn(6),index=rng) ts2.to_period('M') 2000-01 1.368367 2000-01 -0.256934 2000-01 0.417902 2000-02 -1.065910 2000-02 -1.694405 2000-02 0.665471 Freq: M, dtype: float64 3.to_timestamp可以将period转换为timestamp pts.to_timestamp(how='end') 2000-01-31 -0.501502 2000-02-29 -1.299610 2000-03-31 -0.705091 Freq: M, dtype: float64 六、通过数组创建PeriodIndex

某些数据集中时间信息是分开在多个列存放的,可以通过PeriodIndex的参数将这些列组合在一起

year = [2017,2017,2017,2017,2018,2018,2018,2018] quarter = [1,2,3,4,1,2,3,4] index = pd.PeriodIndex(year=year,quarter=quarter,freq='Q-DEC') index PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1', '2018Q2', '2018Q3', '2018Q4'], dtype='period[Q-DEC]', freq='Q-DEC')


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3